﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class ClassTypeEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var uc = new UCClassType { ClassType = value.ToString() };
                var elHost = new ElementHost
                {
                    Width = 480,
                    Height = 360,
                    Child = uc
                };
                edSvc.DropDownControl(elHost);
                return uc.ClassType;
            }
            return value;
        }
    }

    /// <summary>
    /// UCClassType.xaml 的交互逻辑
    /// </summary>
    public partial class UCClassType : UserControl
    {
        private string _classType = "";
        public string ClassType
        {
            get { return _classType; }
            set
            {
                _classType = value;
                Fill();
            }
        }

        public UCClassType()
        {
            InitializeComponent();

            Fill();
        }

        void ck_Checked(object sender, RoutedEventArgs e)
        {
            var ck = (CheckBox)sender;
            _classType = ck.Tag.ToString();
            Fill();
        }

        void Fill()
        {
            spList.Children.Clear();

            var types = DefineFile.LastInstance.FindClassType.GetAllLibType();
            foreach (var type in types)
            {
                var ck = new CheckBox
                {
                    Tag = type,
                    Focusable = false,
                    Content = type,
                    Width = 120,
                    Margin = new Thickness(0, 0, 4, 4),
                    IsChecked = _classType == type,
                    Opacity = _classType == type ? 1 : .5
                };
                ck.Checked += new RoutedEventHandler(ck_Checked);
                ck.Unchecked += new RoutedEventHandler(ck_Checked);
                spList.Children.Add(ck);
            }
        }
    }
}
